package com.weng.cloud.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * 微服务工具类,只有经过微服务网关才有用
 * @author wengzhonghui
 * @date 2019/11/7 11:40
 */
public class CloudAuthUtil {

    // 获取当前登陆用户Id
    public static String getCurUserId(HttpServletRequest request){
        return request.getHeader(CloudConstants.HEADER_AUTH_USER_ID);
    }
    // 获取当前登陆用户账号
    public static String getCurUserAccount(HttpServletRequest request){
        return request.getHeader(CloudConstants.HEADER_AUTH_USER_ACCOUNT);
    }
    // 获取当前登陆用户名称
    public static String getCurUserTrueName(HttpServletRequest request){
        String userName = request.getHeader(CloudConstants.HEADER_AUTH_USER_TRUE_NAME);
        if(userName != null && !"".equalsIgnoreCase(userName.trim())){
            try {
                userName = URLDecoder.decode(userName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
            }
        }
        return userName;
    }

    // 获取当前登陆用户名称
    public static String getCurUserAccountURLDecoder(HttpServletRequest request){
        String userName = request.getHeader(CloudConstants.HEADER_AUTH_USER_ACCOUNT);
        if(userName != null && !"".equalsIgnoreCase(userName.trim())){
            try {
                userName = URLDecoder.decode(userName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
            }
        }
        return userName;
    }


}
